package _18_剑指OfferII;

import _18_剑指OfferII.datastrcute.TreeNode;

import java.util.Stack;

public class _055_剑指OfferII二叉搜索树迭代器2 {

    private TreeNode cur;

    private Stack<TreeNode> stack;

    public _055_剑指OfferII二叉搜索树迭代器2(TreeNode root) {
        cur = root;
        stack = new Stack<>();
    }

    public int next() {
        while (cur != null) {
            stack.push(cur);
            cur = cur.left;
        }
        TreeNode ele = stack.pop();
        cur = ele.right;
        return ele.val;
    }

    public boolean hasNext() {
        return cur != null || !stack.isEmpty();
    }

}
